package com.sk.bilibili.view.activity;

import static com.sk.bilibili.utils.StringUtils.str;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.sk.bilibili.R;
import com.sk.bilibili.adapter.vlayout.main.dynamic.DynamicAdapter;
import com.sk.bilibili.bean.Dynamic;
import com.sk.bilibili.bean.Focus;
import com.sk.bilibili.bean.Search;
import com.sk.bilibili.callBack.DynamicCallBack;
import com.sk.bilibili.callBack.OnBottomCallBack;
import com.sk.bilibili.callBack.OnScrollBottomListener;
import com.sk.bilibili.callBack.TagClickCallBack;
import com.sk.bilibili.callBack.TextChangeCallBack;
import com.sk.bilibili.databinding.ActivityDynamicSearchBinding;
import com.sk.bilibili.model.activity.DynamicSearchModel;

import java.util.List;

public class DynamicSearchActivity extends BaseActivity<ActivityDynamicSearchBinding, DynamicSearchModel> implements TagClickCallBack, OnBottomCallBack, DynamicCallBack {

    public static void start(Context context) {
        Intent intent = new Intent(context, DynamicSearchActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void afterSetView() {
        model.loadData();
        initView();
        bindEvent();
    }

    private void bindEvent() {
        bind.stvShow.setClearListener(v -> model.clearHistory());
        bind.stvShow.setTagClickCallBack(this);
        bind.rvShow.addOnScrollListener(new OnScrollBottomListener(this));
        bind.tvSearch.setOnClickListener(v -> search(str(bind.etSearch)));
        bind.etSearch.addTextChangedListener(new TextChangeCallBack() {
            @Override
            public void afterTextChanged(Editable s) {
                if(s.length()==0)showTip();
            }
        });
    }

    private void showTip() {
        bind.stvShow.setVisibility(View.VISIBLE);
        bind.rvShow.setVisibility(View.GONE);
    }

    private void search(String keyword) {
        bind.etSearch.setText(keyword);
        bind.etSearch.setSelection(keyword.length());
        showList();
        model.search(keyword);
    }

    private void showList() {
        bind.rvShow.setVisibility(View.VISIBLE);
        bind.stvShow.setVisibility(View.GONE);
    }

    private DynamicAdapter dynamicAdapter;

    private void initView() {
        VirtualLayoutManager layoutManager = new VirtualLayoutManager(this);
        bind.rvShow.setLayoutManager(layoutManager);
        DelegateAdapter adapter = new DelegateAdapter(layoutManager);
        dynamicAdapter = new DynamicAdapter(this, this);
        adapter.addAdapter(dynamicAdapter);
        bind.rvShow.setAdapter(adapter);
        bind.rvShow.setVisibility(View.GONE);
    }

    public void loadTipSuccess(List<Search> searches) {
        bind.stvShow.setTip(searches);
    }

    public void loadHistorySuccess(List<Search> searches) {
        bind.stvShow.setHistory(searches);
    }

    @Override
    public void onTagClick(String tag) {
        search(tag);
    }

    public void clearHistorySuccess() {
        bind.stvShow.clearHistory();
    }

    @Override
    public void onBottom() {
        model.loadMore();
    }

    @Override
    public void onMemberClick(Integer id) {
        MemberActivity.start(this,id);
    }

    @Override
    public void onVideoSetClick(Integer id) {
        VideoSetActivity.start(this,id);
    }

    @Override
    public void onThemClick(Integer id) {
        ThemActivity.start(this,id);
    }

    @Override
    public void onForwardClick(int type, Integer targetId) {
        DynamicActivity.start(this,type,targetId);
    }

    @Override
    public void onPraiseClick(int type, Integer targetId) {
        model.praise(type,targetId);
    }

    @Override
    public void onFocusClick(Integer id) {
        model.switchFocus(id);
    }

    public void praiseSuccess() {
        toast("操作成功");
    }

    public void switchFocusSuccess(Focus focus) {
        toast(focus==null?"取消关注成功":"关注成功");
    }

    public void loadSuccess(List<Dynamic> records) {
        dynamicAdapter.setData(records);
    }

    public void loadMoreSuccess(List<Dynamic> records) {
        dynamicAdapter.addData(records);
    }

}